package com.lh.entity.enums;

import com.lh.entity.table.ResponsiblePersons;
import lombok.Getter;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Getter
public enum AreaField {
    CITY_LEVEL("市本级责任人", "city_level_responsible", ResponsiblePersons::getCityLevelResponsible),
    HUANGZHOU("黄州区责任人", "huangzhou", ResponsiblePersons::getHuangzhou),
    TUANFENG("团风县责任人", "tuanfeng", ResponsiblePersons::getTuanfeng),
    HONGAN("红安县责任人", "hongan", ResponsiblePersons::getHongan),
    MACHENG("麻城市责任人", "macheng", ResponsiblePersons::getMacheng),
    LUOTIAN("罗田县责任人", "luotian", ResponsiblePersons::getLuotian),
    YINGSHAN("英山县责任人", "yingshan", ResponsiblePersons::getYingshan),
    XISHUI("浠水县责任人", "xishui", ResponsiblePersons::getXishui),
    QICHUN("蕲春县责任人", "qichun", ResponsiblePersons::getQichun),
    WUXUE("武穴市责任人", "wuxue", ResponsiblePersons::getWuxue),
    HUANGMEI("黄梅县责任人", "huangmei", ResponsiblePersons::getHuangmei),
    LONGGANHU("龙感湖责任人", "longganhu", ResponsiblePersons::getLongganhu),
    GAOXINQU("高新区责任人", "gaoxinqu", ResponsiblePersons::getGaoxinqu),
    LINKONGQU("临空区责任人", "linkongqu", ResponsiblePersons::getLinkongqu),
    BAILIANHE("白莲河责任人", "bailianhe", ResponsiblePersons::getBailianhe);

    private final String labelZh;     // 中文列名
    private final String columnName;  // 表字段名
    private final Function<ResponsiblePersons, String> getter;

    AreaField(String labelZh, String columnName, Function<ResponsiblePersons, String> getter) {
        this.labelZh = labelZh;
        this.columnName = columnName;
        this.getter = getter;
    }

    // 支持用中文/英文字段名来定位
    private static final Map<String, AreaField> LOOKUP =
            Arrays.stream(values()).flatMap(a ->
                    Arrays.asList(
                            new String[]{a.labelZh, a.name(), a.columnName}
                    ).stream().map(k -> new Object[]{k, a})
            ).collect(Collectors.toMap(
                    kv -> ((String) kv[0]).trim(), kv -> (AreaField) kv[1], (a, b) -> a
            ));

    public static AreaField from(String key) {
        if (key == null) return null;
        AreaField f = LOOKUP.get(key.trim());
        if (f != null) return f;
        // 兼容：去掉“责任人”三个字再匹配
        String k2 = key.trim().replace("责任人", "");
        for (AreaField a : values()) {
            if (a.labelZh.replace("责任人","").equals(k2)) return a;
        }
        return null;
    }
}
